package com.zdp.rabbitmq.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @author sesshomaru
 * @date 2019/11/27 11:31
 *
 * 死信队列
 */
@Configuration
public class DeadQueueConfig {

    /**
     * 死信队列 交换机标识符
     */
    private static final String DEAD_LETTER_QUEUE_KEY = "x-dead-letter-exchange";

    /**
     * 死信队列交换机绑定键标识符
     */
    private static final String DEAD_LETTER_ROUTING_KEY = "x-dead-letter-routing-key";

    /**
     * 死信队列跟交换机类型没有关系 不一定为directExchange 不影响该类型交换机的特性.
     */
    @Bean
    public DirectExchange deadLetterExchange(){
        return new DirectExchange("dl_exchange",true,false);
    }

    /**
     * 声明一个死信队列. x-dead-letter-exchange 对应 死信交换机 x-dead-letter-routing-key 对应
     * 常规队列添加 死信时的指向
     * 声明一个 订单队列
     */
    @Bean
    public Queue orderQueue(){
        Map<String,Object> args = new HashMap<>();
        // x-dead-letter-exchange 声明 死信交换机
        args.put(DEAD_LETTER_QUEUE_KEY,"dl_exchange");
        args.put(DEAD_LETTER_ROUTING_KEY,"key_r");
        return new Queue("order_queue",true,false,false,args);
    }

    /**
     * 声明一个订单交换机
     */
    @Bean
    public DirectExchange orderExchange(){
        return new DirectExchange("order_exchange",true,false);
    }

    /**
     * 死信所存放的队列
     */
    @Bean
    public Queue orderDeadLetterQueue(){
        return new Queue("order_dlx",true,false,false);
    }


    @Bean
    public Binding orderBinding(){
        return BindingBuilder.bind(orderQueue()).to(orderExchange()).with("hh");
    }

    @Bean
    public Binding orderDlxBinding(){
        return BindingBuilder.bind(orderDeadLetterQueue()).to(deadLetterExchange()).with("key_r");
    }


}
